package indi.zhifa.engine.redis.utils;


import indi.zhifa.engine.core.common.entity.web.ServiceException;
import indi.zhifa.engine.redis.enums.ERedisType;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisTemplateProvider {

    private final ObjectProvider<RedisTemplate<String, Object>> mNormalRedisTemplateProvider;
    private final ObjectProvider<RedisTemplate<String, Object>> mCacheRedisTemplateProvider;

    public RedisTemplateProvider(ObjectProvider<RedisTemplate<String, Object>> pNormalRedisTemplateProvider,
                                 ObjectProvider<RedisTemplate<String, Object>> pCacheRedisTemplateProvider) {
        mNormalRedisTemplateProvider = pNormalRedisTemplateProvider;
        mCacheRedisTemplateProvider = pCacheRedisTemplateProvider;
    }

    public RedisTemplate<String, Object> getNormalRedisTemplate() {
        RedisTemplate<String, Object> rtn = mNormalRedisTemplateProvider.getObject();
        if(rtn == null) {
            throw new ServiceException("没有实例化NormalRedisTemplate，请查看配置");
        }
        return rtn;
    }

    public RedisTemplate<String, Object> getCacheRedisTemplate() {
        RedisTemplate<String, Object> rtn = mCacheRedisTemplateProvider.getObject();
        if(rtn == null) {
            throw new ServiceException("没有实例化cacheRedisTemplate，请查看配置");
        }
        return rtn;
    }

    public RedisTemplate<String, Object> getRedisTemplate(ERedisType pERedisType){
        switch (pERedisType){
            case CACHE,SESSION->{
                return getCacheRedisTemplate();
            }
            default -> {
                return getNormalRedisTemplate();
            }
        }
    }

}
